<?php
include '../includes/cPage.php';
include '../includes/cDatabase.php';
include '../includes/variables.php';
include '../includes/cPlayer.php';
include '../includes/cMember.php';
include '../includes/cGame.php';
include '../includes/cTransfer.php';
include '../includes/cDates.php';
include '../includes/cInjury.php';

$cPage = new page("Injured Players");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

$proxy_name="137.58.62.9";
$proxy_port=8080;
$url = "http://www.physioroom.com/news/english_premier_league/epl_injury_table.php";

$cDatabase = new database();

$connect = $cDatabase->connectToDatabase();
$activeDatabase = $cDatabase->getActiveDatabase($connect);

$db = $cDatabase->selectDatabase($connect, $activeDatabase);

$result = $cDatabase->DoesTableExist($connect, 'injury');
if ($result == FALSE)
{
	// Create this table
	$cDatabase->createInjuryTable($connect);
}
else
{
	//echo "<BR>Table exists";
}
$cDatabase->deleteTableEntries($connect, 'injury');

for ($i = 0; $i < 20; $i++)
{
	$teamInjury[$i] = new injury();
}

$count = preadfilet($url, $proxy_name, $proxy_port, $teamInjury);

// Add a row to the table
for ($i = 0; $i < 20; $i++)
{
	for ($playerIndex = 0; $playerIndex < $teamInjury[$i]->numberInjuredPlayers; $playerIndex++)
	{
		$cDatabase->insertInjury($connect, 
			$teamInjury[$i]->playerName[$playerIndex], 
			$teamInjury[$i]->teamName, 
			$teamInjury[$i]->injuryName[$playerIndex], 
			$teamInjury[$i]->returnDate[$playerIndex]);
	}
}

// Search from
// <div class="mvb">
// to 
// <!-- E IINC -->
	
$offset = 0;		
			
function preadfilet($_url, $_proxy_name, $_proxy_port, $teamInjury)
{ 
    $proxy_cont = '';	// Storage area for file contents

    $proxy_fp = pfopent($_url, $_proxy_name, $_proxy_port); 
    while(!feof($proxy_fp)) 
	{
		$proxy_cont .= fread($proxy_fp,4096);
	} 
    fclose($proxy_fp); 

	//$start = strpos($proxy_cont,"Reported English");
	$start = strpos($proxy_cont,"<div id=\"inta_new_wrap\">");
	//$end = strpos($proxy_cont,"http://www.physioroom.com/news/english_premier_league/syndicates")-25;
	$end = strpos($proxy_cont,"</table></div>") + 15;
	
	//$start = strpos($proxy_cont,"Reported English");
	//$end = strpos($proxy_cont,"http://www.physioroom.com/news/english_premier_league/syndicates")-25;
	$length = $end - $start;

	$proxy_cont = substr($proxy_cont, $start, $length);
    echo $proxy_cont;

	ScanFileForTeams($proxy_cont, $teamInjury);

    return count($proxy_cont); 
} 

function pfopent($_url, $_proxy_name, $_proxy_port) 
{ 
    $proxy_fp = fsockopen($_proxy_name, $_proxy_port); 
    if (!$proxy_fp)
	{
		return false;
	}
    $host= substr($_url, 7); 
    $host = substr($bucket, 0, strpos($host, "/")); 

    $request = "GET $_url HTTP/1.0\r\nHost:$host\r\n\r\n"; 

    fputs($proxy_fp, $request); 

    return $proxy_fp;  
}

function ScanFileForTeams($proxy_cont, $teamInjury)
{	
	$team = array();

	// Search for _injuries.html">Arsenal</a></td>
	
	$teamStart = strpos($proxy_cont,"_injuries.html") + 16;
	$teamEnd = strpos($proxy_cont,"</a></td>", $start);
	$lastEnd = 0;
		
	for ($teamIndex = 0; $teamIndex < 20; $teamIndex++)
	{
		// Get team name
		$teamStart = strpos($proxy_cont,"_injuries.html", $lastEnd) + 16;
		$teamEnd = strpos($proxy_cont,"</a></td>", $teamStart);
		$teamLength = $teamEnd - $teamStart;
		
		//echo "<BR>team start $teamStart end $teamEnd length $teamLength ";
		$webTeamName = substr($proxy_cont, $teamStart, $teamLength);
		
		$teamInjury[$teamIndex]->setTeamName($webTeamName);
		
	    //echo "<BR>".$teamInjury[$teamIndex]->teamName;
		
		// Get number of injured players
		// <td class="aligncenter">7</td>
		$numberStart = strpos($proxy_cont,"<td class=", $teamEnd)+24;
		$numberEnd = strpos($proxy_cont,"</td>", $numberStart);
		$numberLength = $numberEnd - $numberStart;
		
		//echo "<BR>loop start $numberStart end $numberEnd length $numberLength ";
		$teamInjury[$teamIndex]->numberInjuredPlayers = substr($proxy_cont, $numberStart, $numberLength);
	    //echo "loopLength ".$teamInjury[$teamIndex]->numberInjuredPlayers;
		
		// Get players' details
		$lastPlayerEnd = $numberEnd;
		
		for ($playerIndex = 0; $playerIndex < $teamInjury[$teamIndex]->numberInjuredPlayers; $playerIndex++)
		{
			//echo "<BR><BR>Loop $playerIndex / ".$teamInjury[$teamIndex]->numberInjuredPlayers;
			// get Player Name
			// <td nowrap="nowrap">M Arteta</td>
			$playerStart = strpos($proxy_cont,"<td nowrap=", $lastPlayerEnd)+20;
			$playerEnd = strpos($proxy_cont,"</td>", $playerStart);
			$playerLength = $playerEnd - $playerStart;
			
			//echo "<BR>player start $playerStart end $playerEnd length $playerLength ";
			
			$teamInjury[$teamIndex]->setPlayerName($playerIndex, substr($proxy_cont, $playerStart, $playerLength));	
			
			//echo "<BR>".$teamInjury[$teamIndex]->playerName[$playerIndex];
			
			// Get Injury
			// <td><a href="http://www.physioroom.com/injuries/hip_and_thigh/hamstring_strain_sum.php">Hamstring Injury</a></td>
			// or
			// <td>Virus</td>
			
			// arbitrary 25 length should be ok
			$arefPosition = strpos(substr($proxy_cont, $playerEnd, 25), "<a href");

			if ($arefPosition == true)
			{
				// If there is a link <a href...
				$injuryStart = strpos($proxy_cont,"php", $playerEnd)+5;
				$injuryEnd = strpos($proxy_cont,"</a></td>", $injuryStart);
				$injuryLength = $injuryEnd - $injuryStart;
			}
			else
			{
				// else it's within <td> </td>
				$injuryStart = strpos($proxy_cont,"<td>", $playerEnd)+5;
				$injuryEnd = strpos($proxy_cont,"</td>", $injuryStart);
				$injuryLength = $injuryEnd - $injuryStart;
			}
			//echo "<BR>injury start $injuryStart end $injuryEnd length $injuryLength ";
			$teamInjury[$teamIndex]->injuryName[$playerIndex] = str_replace("/", "_", substr($proxy_cont, $injuryStart, $injuryLength));

			
			//$teamInjury[$teamIndex]->injuryName[$playerIndex] = substr($proxy_cont, $injuryStart, $injuryLength);
			//echo " injuryName:".$teamInjury[$teamIndex]->injuryName[$playerIndex];
			
			// Get return date
			// <td class="alignright" nowrap="nowrap">30th Oct 10</td>
			$dateStart = strpos($proxy_cont,"\">", $injuryEnd)+2;
			$dateEnd = strpos($proxy_cont,"</td>", $dateStart);
			$dateLength = $dateEnd - $dateStart;
			
			//echo "<BR>date start $dateStart end $dateEnd length $dateLength ";
			$teamInjury[$teamIndex]->returnDate[$playerIndex] = substr($proxy_cont, $dateStart, $dateLength);
			//echo " return date:".$teamInjury[$teamIndex]->returnDate[$playerIndex];
		
			// Skip over all nbsp to get to the next player name
			// <td nowrap="nowrap">&nbsp;</td>
			$nbspStart = strpos($proxy_cont,"nbsp", $dateEnd);
			
			$lastPlayerEnd = $nbspStart;
		}
		
		$lastEnd = $teamEnd;
	}
}

echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?>